<?php

namespace common\models;

use Yii;
use yii\db\ActiveRecord;

/**
 * This is the model class for table "nxh_category".
 *
 * @property integer $id
 * @property integer $parent_id
 * @property string $category_name
 * @property string $description
 * @property integer $status_flg
 * @property integer $create_time
 * @property integer $update_time
 * @property integer $del_flg
 */
class Category extends ActiveRecord
{
    /**
     * @table name
     */
    public static function tableName()
    {
        return 'nxh_category';
    }

    /**
     * time create and update
     * @return array
     */
    public function behaviors()
    {
        return [
            'timestamp' => [
                'class' => 'yii\behaviors\TimestampBehavior',
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['create_time', 'update_time'],
                    ActiveRecord::EVENT_BEFORE_UPDATE => ['update_time'],
                ],
            ],
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['parent_id', 'category_name'], 'required'],
            [['parent_id', 'status_flg', 'create_time', 'update_time', 'del_flg'], 'integer'],
            [['description'], 'string'],
            [['category_name'], 'string', 'max' => 255]
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'parent_id' => Yii::t('app', 'Parent ID'),
            'category_name' => Yii::t('app', 'Category Name'),
            'description' => Yii::t('app', 'Description'),
            'status_flg' => Yii::t('app', 'Status Flg'),
            'create_time' => Yii::t('app', 'Create Time'),
            'update_time' => Yii::t('app', 'Update Time'),
            'del_flg' => Yii::t('app', 'Del Flg'),
        ];
    }

    public static function getParentList($listData = false){
        $dataQuery = static::find()
            ->where(['del_flg' => \Constant::DEL_FALSE, 'parent_id' => 0])->all();
        if(false===$listData)
            return $dataQuery;
        else{
            $result[] = \Yii::t('app', '-- Root --');
            foreach($dataQuery as $data){
                $result[$data->id] = $data->category_name;
            }
            return $result;
        }
    }
}
